//
//  ViewController.m
//  03_UIGestureRecongnizer
//
//  Created by 徐昊 on 16/7/18.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 手势识别器
    // 分为7种
    // 轻拍 Tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发方法, 默认为1
    tap.numberOfTapsRequired = 2;
    // 几个手指触发 (按option - alt) 默认为1
    tap.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:tap];
    // 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始, 默认 0.5s
    longPress.minimumPressDuration = 1;
    // 默认10像素, 长按失败前, 最大的移动距离
    longPress.allowableMovement = 1000;
    [self.view addGestureRecognizer:longPress];
    // 轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // direction可以设置多个, 前提, 多个方向所触发的行为要相同
    // 如果多个枚举方向的行为不同, 需要创建多个清扫的手势
    // 左右与上下有冲突
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    // 练习: ImageView添加清扫换图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    // 将图片的交互打开
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"guidePage_01"];
    [self.view addSubview:imageView];
    //    UISwipeGestureRecognizer *swipeImage = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(imageSwipe:)];
    //     swipeImage.direction = UISwipeGestureRecognizerDirectionRight;
    //[imageView addGestureRecognizer:swipeImage];
    // 平移 pan
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    // pan.minimumNumberOfTouches = 2;
    [imageView addGestureRecognizer:pan];
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    
    [self.view addGestureRecognizer:pinch];
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    // 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screen.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screen];
}
- (void)imageSwipe:(UISwipeGestureRecognizer *)swipeChange {
    // 按位枚举 4个选项 -> 1 1 1 1
    // 全选情况 1(3) 1(2) 1(1) 1(0)
    if (swipeChange.direction & UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"111");
    }
    UIImageView *imageView = (UIImageView *)swipeChange.view;
    imageView.image = [UIImage imageNamed:@"guidePage_03.png"];
}
// 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
// 长按
- (void)longPressAction:(UILongPressGestureRecognizer *) longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
    NSLog(@"长按");
}
// 清扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"清扫");
}
// 平移
- (void)panAction:(UIPanGestureRecognizer *) pan {
    NSLog(@"平移");
    if (pan.state == UIGestureRecognizerStateEnded) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 平移中
        // 平移手势, 根据其View上移动的过程, 得到最终的点
        CGPoint point = [pan translationInView:pan.view];
        // 根据点, 使pan.view移动
        NSLog(@"%@", NSStringFromCGPoint(point));
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan 移动的点置空
        [pan setTranslation:point inView:pan.view];
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        // 平移结束
        pan.view.transform = CGAffineTransformIdentity;
    }
}
// 捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    NSLog(@"%f", pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 设置缩放倍数
    pinch.scale = 1;
}
// 旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotain {
    // 转换中, 根据上次转换继续改变
    rotain.view.transform = CGAffineTransformRotate(rotain.view.transform, rotain.rotation);
    // 旋转的弧度为0
    rotain.rotation = 0;
}
- (void) screenAction:(UIScreenEdgePanGestureRecognizer *) screen {
    
    NSLog(@"屏幕边缘");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
